using System;
using System.Collections.Generic;
using System.Text;

namespace MyWords
{
    public class WordList : System.Collections.Generic.List<Word>
    {
        public void SortByCreatedDate()
        {
            this.Sort(Word.SortByCreatedDate);
        }

        public void SortByHead()
        {
            this.Sort(Word.SortByHead);
        }

    }

    public class Word
    {
        public static int SortByCreatedDate(Word a, Word b)
        {
            return a.CreatedDate.CompareTo(b.CreatedDate);
        }

        public static int SortByHead(Word a, Word b)
        {
            return a.Head.CompareTo(b.Head);
        }

        
        private string head;
        public string Head
        {
            get { return head; }
            set { head = value; }
        }

        private string meaning;
        public string Meaning
        {
            get { return meaning; }
            set { meaning = value; }
        }

        private DateTime createdDate;
        public DateTime CreatedDate
        {
            get { return createdDate; }
            set { createdDate = value; }
        }

        private AudioFormat audio;
        public AudioFormat Audio
        {
            get { return audio; }
            set { audio = value; }
        }

        private byte[] audioBytes;
        public byte[] AudioBytes
        {
            get { return audioBytes; }
            set { audioBytes = value; }
        }

        private System.Collections.Generic.List<Schedule> schedules;
        public System.Collections.Generic.List<Schedule> Schedules
        {
            get { return schedules; }
            set { schedules = value; }
        }

        public Word()
        {
            this.head = "";
            this.meaning = "";
            this.audio = AudioFormat.Unknown;
            this.audioBytes = null;
            this.schedules = new System.Collections.Generic.List<Schedule>();
            this.CreatedDate = DateTime.Now;
        }

        public Word(string head, string meaning)
        {
            this.head = head;
            this.meaning = meaning;
            this.audio = AudioFormat.Unknown;
            this.audioBytes = null;
            this.schedules = new System.Collections.Generic.List<Schedule>();
            this.CreatedDate = DateTime.Now;
        }

        public Word(string head, string meaning, DateTime dt)
        {
            this.head = head;
            this.meaning = meaning;
            this.audio = AudioFormat.Unknown;
            this.audioBytes = null;
            this.schedules = new System.Collections.Generic.List<Schedule>();
            this.CreatedDate = dt;
        }

        public override string ToString()
        {
            return this.head;
        }

        private int NextStudyScheduleIndex()
        {
            for (int i=0; i<this.schedules.Count; i++)
            {
                if (this.schedules[i].StudyDate.IsNull)
                {
                    return i;
                }
            }
            return -1;
        }

        public void UpdateStudyInfo(DateTime StudyDateTime)
        {
            int next = NextStudyScheduleIndex();
            if (next == -1) return;

            double lateDays = ((TimeSpan)(StudyDateTime.Date - this.schedules[next].ScheduleDate.Value)).TotalDays;
            if (lateDays >= 0)
            {
                this.schedules[next].StudyDate = new System.Data.SqlTypes.SqlDateTime(StudyDateTime);
                this.schedules[next].ScheduleDate = new System.Data.SqlTypes.SqlDateTime(StudyDateTime.Date);
                for (int i = next+1; i < this.schedules.Count; i++)
                {
                    this.schedules[i].ScheduleDate = new System.Data.SqlTypes.SqlDateTime(this.schedules[i].ScheduleDate.Value.AddDays(lateDays));
                }
            } else 
            {
                // change nothing
            }
        }

        private int LastStudiedIndex()
        {
            for (int i = this.schedules.Count-1; i >=0 ; i--)
            {
                if (!this.schedules[i].StudyDate.IsNull)
                {
                    return i;
                }
            }
            return -1;
        }

        //public void DeleteStudyInfo(DateTime aDay )
        //{
        
        //    int last = LastStudiedIndex();
        //    if (last == -1) return;

        //    if (aDay.Date != this.schedules[last].StudyDate.Value.Date) return;
        //    this.schedules[last].StudyDate = new System.Data.SqlTypes.SqlDateTime();

        //    //double lateDays = ((TimeSpan)(aDay.Date - this.schedules[last].ScheduleDate.Value)).TotalDays;

        //    //if (lateDays >= 0)
        //    //{
        //    //    this.schedules[last].StudyDate = new System.Data.SqlTypes.SqlDateTime();
        //    //    this.schedules[last].ScheduleDate = new System.Data.SqlTypes.SqlDateTime(StudyDateTime.Date);

        //    //    for (int i = last + 1; i < this.schedules.Count; i++)
        //    //    {
        //    //        this.schedules[i].ScheduleDate = new System.Data.SqlTypes.SqlDateTime(this.schedules[i].ScheduleDate.Value.AddDays(-lateDays));
        //    //    }
        //    //}
        //    //else
        //    //{
        //    //    // change nothing
        //    //}
        //}
    }

    public class Schedule
    {
        private System.Data.SqlTypes.SqlDateTime scheduleDate;

        public System.Data.SqlTypes.SqlDateTime ScheduleDate
        {
            get { return scheduleDate; }
            set { scheduleDate = value; }
        }

        private System.Data.SqlTypes.SqlDateTime studyDate;

        public System.Data.SqlTypes.SqlDateTime StudyDate
        {
            get { return studyDate; }
            set { studyDate = value; }
        }
        public Schedule(System.Data.SqlTypes.SqlDateTime scheduleDate, System.Data.SqlTypes.SqlDateTime studyDate)
        {
            this.scheduleDate = scheduleDate;
            this.studyDate = studyDate;
        }
        public Schedule(DateTime scheduleDate)
        {
            this.scheduleDate = new System.Data.SqlTypes.SqlDateTime(scheduleDate);
            this.studyDate = new System.Data.SqlTypes.SqlDateTime();
        }
        public Schedule(DateTime scheduleDate, DateTime studyDate)
        {
            this.scheduleDate = new System.Data.SqlTypes.SqlDateTime(scheduleDate);
            this.studyDate = new System.Data.SqlTypes.SqlDateTime(studyDate);
        }
    }

    public enum AudioFormat
    {
        Unknown,
        None,
        Original,
        GZip
    }
}
